<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最长公共子序列</title>
</head>

<body>
  <script>
    var longestCommonSubsequence = function (text1, text2) {
      let m = text1.length;
      let n = text2.length;
      let dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
      dp[0][0] = 0;
      for (let i = 1; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
          if (text1[i - 1] === text2[j - 1]) {
            dp[i][j] = dp[i - 1][j - 1] + 1;
          } else {
            dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
          }
        }
      }
      return dp[m][n];
    };
    console.log(longestCommonSubsequence("abcde", "ace"));
  </script>
</body>

</html>